package com.situ.mall.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class ServiceLogAspect {
    private static final Logger LOGGER = LoggerFactory.getLogger(ServiceLogAspect.class);

    // 这个包下所有对象的所有方法，不关心返回值
    @Around("execution(* com.situ.mall.service.*.*(..))")
    public Object recordTimeLog(ProceedingJoinPoint joinPoint) throws Throwable {
        LOGGER.info("========开始执行{}.{}========", joinPoint.getTarget().getClass(), joinPoint.getSignature().getName());
        // 记录开始时间
        long begin = System.currentTimeMillis();
        // 执行目标对象Service中代码
        Object result = joinPoint.proceed();
        long end = System.currentTimeMillis();
        long takeTime = end - begin;
        if (takeTime > 3000) {
            LOGGER.error("========执行时间为  {}  毫秒========", takeTime);
        } else if (takeTime > 2000) {
            LOGGER.warn("========执行时间为  {}  毫秒========", takeTime);
        } else {
            LOGGER.info("========执行时间为  {}  毫秒========", takeTime);
        }
        return result;
    }
}
